package com.ucar.train.web;

import com.google.common.base.Strings;
import com.ucar.train.dto.*;
import com.ucar.train.service.CrmService;
import com.ucar.train.service.impl.CarServiceImp;
import com.ucar.train.service.impl.OrderDetailServiceImpl;
import com.ucar.train.service.impl.OrderServiceImpl;
import com.ucar.train.service.impl.StoreServiceImpl;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.vo.CarVO;
import com.ucar.train.vo.OrderDetailVO;
import com.ucar.train.vo.OrderVO;
import com.ucar.train.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: OrderController
 * @Description TODO
 * @Author: Mingzhi.Xu
 * @Create: 2019-08-14 11:06
 **/
@Slf4j
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private OrderDetailServiceImpl orderDetailService;

    @Autowired
    CrmService crmService;

    @Autowired
    private CarServiceImp carService;

    @Autowired
    private StoreServiceImpl storeService;

    /**
     * 功能描述: <br>
     * 〈 查询所有订单基本信息 〉
     *
     * @Param: []
     * @Return: org.springframework.web.servlet.ModelAndView
     * @Author: LiuHao
     * @Date: 2019/8/14 13:17
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public ModelAndView selectAll() {
        ModelAndView modelAndView = new ModelAndView();
        List<OrderVO> orderVOS = new ArrayList<>();//
        List<OrderDTO> orderDTOS = orderService.selectAll();//查询所有基本信息
        for (OrderDTO orderDTO : orderDTOS) {
            OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderDTO.getOrderNo());//查询当前订单详细信息
            OrderVO orderVO = new OrderVO();
            CrmDTO crmDTO = crmService.selectByCustomerID(orderDTO.getCrmId());//通过crmId查出姓名
            CarDTO carDTO = carService.selectCarDTOByCarId(orderDetailDTO.getCarId());//通过carID查出车类型
            StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
            StoreDTO backStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getBackStoreCode());
            if (!Strings.isNullOrEmpty(orderDetailDTO.getGetStoreCode())) {
                orderVO.setGetStoreCode(getStoreStoreDTO.getName());
            } else {
                orderVO.setGetStoreCode("暂未取车");
            }
            if (!Strings.isNullOrEmpty(orderDetailDTO.getBackStoreCode())) {
                orderVO.setBackStoreCode(backStoreStoreDTO.getName());
            } else {
                orderVO.setBackStoreCode("暂未还车");
            }

            orderVO.setOrderNo(orderDTO.getOrderNo());
            orderVO.setCrmName(crmDTO.getUsername());//调CRM接口
            orderVO.setCarNumber(orderDTO.getCarNumber());
            orderVO.setType(carDTO.getType());//调Car接口


            orderVO.setGetTime(JodaUtils.dateToString(orderDetailDTO.getGetTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setBackTime(JodaUtils.dateToString(orderDetailDTO.getBackTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setRent(orderDTO.getRent());
            orderVOS.add(orderVO);
        }
        modelAndView.addObject("orderList", orderVOS);
        modelAndView.setViewName("order-list");
        return modelAndView;
    }

    /**
     * 功能描述: <br>
     * 〈查询当前订单详细信息〉
     *
     * @Param: [OrderNo]
     * @Return: org.springframework.web.servlet.ModelAndView
     * @Author: XuMingZhi
     * @Date: 2019/8/14 14:12
     */
    @RequestMapping(value = "/selectDetailByOrderNo", method = RequestMethod.GET)
    public ModelAndView selectDetailByOrderNo(@RequestParam(name = "orderNo", required = true, defaultValue = "CNO1970010100000000001") String orderNo) {
        ModelAndView modelAndView = new ModelAndView();
        OrderDTO orderDTO = orderService.selectByOrderNo(orderNo);
        OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderNo);
        CarDTO carDTO = carService.selectCarDTOByCarId(orderDetailDTO.getCarId());
        StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
        StoreDTO backStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getBackStoreCode());
        log.info(orderDTO.toString());
        OrderVO orderVO = new OrderVO();
        if (!Strings.isNullOrEmpty(orderDetailDTO.getGetStoreCode())) {
            orderVO.setGetStoreCode(getStoreStoreDTO.getName());
        } else {
            orderVO.setGetStoreCode("暂未取车");
        }
        if (!Strings.isNullOrEmpty(orderDetailDTO.getBackStoreCode())) {
            orderVO.setBackStoreCode(backStoreStoreDTO.getName());
        } else {
            orderVO.setBackStoreCode("暂未还车");
        }
        orderVO.setId(orderDTO.getId());
        orderVO.setOrderNo(orderDTO.getOrderNo());
        orderVO.setCarNumber(orderDTO.getCarNumber());
        orderVO.setType(carDTO.getType());//调Car接口
        orderVO.setGetTime(JodaUtils.dateToString(orderDetailDTO.getGetTime(), "yyyy-MM-dd HH:mm:ss"));
        orderVO.setBackTime(JodaUtils.dateToString(orderDetailDTO.getBackTime(), "yyyy-MM-dd HH:mm:ss"));
        orderVO.setRent(orderDTO.getRent());
        orderVO.setCost(orderDTO.getCost());
        orderVO.setState(orderDTO.getState());
        orderVO.setCarComment(orderDetailDTO.getCarComment());
        orderVO.setStoreComment(orderDetailDTO.getStoreComment());
        modelAndView.addObject("order", orderVO);
        modelAndView.setViewName("order-show");
        return modelAndView;
    }

    /**
     * 功能描述: <br>
     * 〈查询当前客户所有订单信息〉///给CRM完成
     *
     * @Param: [CrmId]
     * @Return: org.springframework.web.servlet.ModelAndView
     * @Author: XuMingZhi
     * @Date: 2019/8/14 14:14
     */
    @RequestMapping(value = "/selectOrdersByCrmId", method = RequestMethod.GET)
    public ModelAndView selectOrdersByCrmId(@RequestParam(name = "CrmId", required = true) Long CrmId) {

        ModelAndView modelAndView = new ModelAndView();
        List<OrderDTO> orderDTOS = orderService.SelectOrdersByCustomerId(CrmId);
        List<OrderVO> orderVOS = new ArrayList<>();
        //
        for (OrderDTO orderDTO : orderDTOS) {
            OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderDTO.getOrderNo());
            CarDTO carDTO = carService.selectCarDTOByCarId(orderDetailDTO.getCarId());
            StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
            StoreDTO backStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getBackStoreCode());
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderNo(orderDTO.getOrderNo());
            orderVO.setCarNumber(orderDTO.getCarNumber());
            orderVO.setType(carDTO.getType());//调Car接口
            if (!Strings.isNullOrEmpty(orderDetailDTO.getGetStoreCode())) {
                orderVO.setGetStoreCode(getStoreStoreDTO.getName());
            } else {
                orderVO.setGetStoreCode("暂未取车");
            }
            if (!Strings.isNullOrEmpty(orderDetailDTO.getBackStoreCode())) {
                orderVO.setBackStoreCode(backStoreStoreDTO.getName());
            } else {
                orderVO.setBackStoreCode("暂未还车");
            }
            orderVO.setGetTime(JodaUtils.dateToString(orderDetailDTO.getGetTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setBackTime(JodaUtils.dateToString(orderDetailDTO.getBackTime(), "yyyy-MM-dd HH:mm:ss"));
            orderVO.setRent(orderDTO.getRent());
            orderVO.setCost(orderDTO.getCost());
            orderVO.setState(orderDTO.getState());
            orderVOS.add(orderVO);
        }

        modelAndView.addObject("orders", orderVOS);
        modelAndView.setViewName("customer-show");
        return modelAndView;
    }

    /**
     * 功能描述: <br>
     * 〈更新评论〉
     *
     * @Param: [orderVO]
     * @Return: java.lang.String
     * @Author: XuMingZhi
     * @Date: 2019/8/14 14:20
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(OrderVO orderVO, RedirectAttributes redirectAttributes) {
        OrderDetailDTO orderDetailDTO = orderDetailService.selectByOrderNo(orderVO.getOrderNo());
        if (!Strings.isNullOrEmpty(orderVO.getCarComment())) {
            orderDetailDTO.setCarComment(orderVO.getCarComment());
        }
        if (!Strings.isNullOrEmpty(orderVO.getStoreComment())) {
            orderDetailDTO.setCarComment(orderVO.getStoreComment());
        }
        orderDetailDTO.setCarComment(orderVO.getCarComment());
        orderDetailDTO.setStoreComment(orderVO.getStoreComment());
        orderDetailService.updateByPrimaryKeySelective(orderDetailDTO);
        redirectAttributes.addAttribute("orderNo", orderDetailDTO.getOrdNo());
        return "redirect:selectDetailByOrderNo";

    }

    /**
     * 功能描述: 个人或企业下单
     * 〈〉
     * @Param: [orderVO, pay]
     * @Return: org.springframework.web.servlet.ModelAndView
     * @Author: zjm
     * @Date: 2019/8/20 16:22
     */
    @RequestMapping(value = "/addOrder")
    public String addOrder(OrderVO orderVO, @RequestParam("pay") Boolean pay) {
        System.out.println(orderVO);
        String state = "待支付";
        if (pay.equals(true)) {
            state = "已支付";
        }
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setCrmId(orderVO.getCrmId());
        orderDTO.setCarNumber(orderVO.getCarNumber());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date backTime = simpleDateFormat.parse(orderVO.getBackTime());
            orderDTO.setBackTime(backTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        orderDTO.setStoreCode(orderVO.getGetStoreCode());
        orderDTO.setRent(orderVO.getRent());
        orderDTO.setState(state);
        BigDecimal cost = orderVO.getCost();
        Boolean successful = orderService.addOrder(orderDTO, orderVO.getNumber(), orderVO.getCarType(), cost);

        if(successful) {
           return  "redirect:main";
        } else {
            return "redirect: order-error";
        }
    }

    /**
     * 功能描述: <br>
     * 〈根据门店查询评论〉
     * @Param: [storeId]
     * @Return: org.springframework.web.servlet.ModelAndView
     * @Author: XuMingZhi
     * @Date: 2019/8/27 13:58
     */
    @RequestMapping(value = "/selectStoreCommentByStoreId",method = RequestMethod.GET)
    public ModelAndView selectStoreCommentByStoreId(@RequestParam(name = "storeId", required = true, defaultValue = "A001") String storeId) {
        List<StoreDTO> storeDTOS = storeService.selectAllStore();
        List<StoreVO> storeVOS=new ArrayList<>(storeDTOS.size());
        for (StoreDTO storeDTO : storeDTOS) {
            StoreVO storeVo=new StoreVO();
            storeVo.setId(storeDTO.getId());
            storeVo.setStoreCode(storeDTO.getStoreCode());
            storeVo.setName(storeDTO.getName());
            storeVOS.add(storeVo);
        }
        StoreVO storeVO=new StoreVO();
        StoreDTO storeDTO = storeService.selectStorebyStoreId(storeId);
        storeVO.setName(storeDTO.getName());
        storeVO.setStoreCode(storeId);
        List<OrderDetailDTO> orderDetailDTOS=orderDetailService.selectByGetStoreCode(storeId);
        List<OrderDetailVO> orderDetailVOS=new ArrayList<>(orderDetailDTOS.size());
        for(OrderDetailDTO orderDetailDTO:orderDetailDTOS){
            OrderDetailVO orderDetailVO=new OrderDetailVO();
            StoreDTO getStoreStoreDTO = storeService.selectStorebyStoreId(orderDetailDTO.getGetStoreCode());//通过storeCode查出门店信息
            orderDetailVO.setStoreName(getStoreStoreDTO.getName());
            orderDetailVO.setStoreComment(orderDetailDTO.getStoreComment());
            orderDetailVOS.add(orderDetailVO);
        }
        storeVO.setOrderDetailVOList(orderDetailVOS);

        //ModelAndView 添加VO
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("storeVOS", storeVOS);
        modelAndView.addObject("stores", storeVO);
        modelAndView.setViewName("storecomment-list");
        //返回ModelAndView
        return modelAndView;
    }

    @RequestMapping(value = "/carComment" )
    public ModelAndView addOrder(@RequestParam(name = "carId", defaultValue = "A002001") String carId)
    {

        List<OrderDetailDTO> carCommentList = orderDetailService.selectCarCommentByCarId(carId);
        CarDTO carDTO1 = carService.selectCarDTOByCarId(carId);
        CarVO carVO=new CarVO();
        carVO.setCarId(carId);
        carVO.setType(carDTO1.getType());
        carVO.setCarNum(carDTO1.getCarNum());
        List<String> carVOListComment=new ArrayList<>();
        for (OrderDetailDTO carComment : carCommentList) {
            carVOListComment.add(carComment.getCarComment());
        }
        carVO.setComment(carVOListComment);
        List<CarDTO> carDTOS = carService.selectAllCar();
        List<CarVO> carVOList=new ArrayList<>(carDTOS.size());
        for (CarDTO carDTO : carDTOS) {
            CarVO carVO1=new CarVO();
            carVO1.setCarId(carDTO.getCarId());
            carVO1.setType(carDTO.getType());
            carVO1.setCarNum(carDTO.getCarNum());
            carVOList.add(carVO1);
        }
        //ModelAndView 添加VO
        ModelAndView modelAndView = new ModelAndView();
        //指定车的评论carVO
        modelAndView.addObject("stores", carVO);
        //所有车的VO,carVOList
        modelAndView.addObject("storeVOS", carVOList);
        modelAndView.setViewName("carcomment-list");
        //返回ModelAndView
        return modelAndView;
    }

}
